package cn.zcx;

import java.util.concurrent.BrokenBarrierException;
import java.util.concurrent.CyclicBarrier;
import java.util.concurrent.TimeUnit;

/**
 * description:
 * author:zcx
 * lenovo
 * 时间：2022.02.07.19.15
 */
public class T14 {
    CyclicBarrier c=new CyclicBarrier(3,()->{
        System.out.println("到齐了，开饭了。。%n");
    });
    public static void main(String[] args) {
        var t=new T14();
        new Thread(()-> t.eat(6),"张三").start();
        new Thread(()-> t.eat(15),"李四").start();
        new Thread(()-> t.eat(25),"王五").start();

    }
    void eat(int s){
        String t=Thread.currentThread().getName();
        System.out.printf("%s,开始动身，需要%d少秒到达%n",t,s);
        try {
            TimeUnit.SECONDS.sleep(s);
            System.out.printf("%s,已到达,%n",t);
            c.await();
            System.out.println(t);
        } catch (InterruptedException | BrokenBarrierException e) {
            e.printStackTrace();
        }
    }

}
